import time

from selenium import webdriver
from selenium.webdriver.common.by import By


class Operation:
    def driver(self):
        driver = webdriver.Chrome()
        driver.get('http://localhost:90')
        driver.maximize_window()
        return driver

    def asserts(self, result, expect, driver):
        if result == expect:
            print('success')
        else:
            driver.save_screenshot('filename.png')
            print('failed')
        assert result == expect

    def login(self, driver, username, password):
        driver.find_element(By.XPATH, '//*[@id="username"]').send_keys(username)
        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(password)
        driver.find_element(By.XPATH, '//*[@id="login_btn"]').click()

    def signup(self, driver, username, password, repassword):
        if password is None:
            password = ''
        if repassword is None:
            repassword = ''
        driver.find_element(By.XPATH, '/html/body/div/div/div/div[2]/a').click()
        driver.find_element(By.XPATH, '//*[@id="username"]').send_keys(username)
        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(password)
        driver.find_element(By.XPATH, '//*[@id="repassword"]').send_keys(repassword)
        driver.find_element(By.XPATH, '//*[@id="login_btn"]').click()

    def switch_bankcard(self, driver):
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "个人中心").click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "银行卡管理").click()
        time.sleep(1)

    def return_bankcard(self, driver):
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "个人中心").click()
        time.sleep(1)

    def bankcard_count(self, driver):
        self.switch_bankcard(driver)
        result = len(driver.find_elements(By.XPATH, "//*[@class='col-sm-12']"))
        self.return_bankcard(driver)
        return result

    def bankcard_add(self, driver, bankcard_name, bankcard_type, bankcard_num):
        self.switch_bankcard(driver)
        driver.find_element(By.XPATH, '//*[@id="bankCard_add_modal_btn"]').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="cardBank_add_input"]').send_keys(bankcard_name)
        if bankcard_type == '借记卡':
            driver.find_elements(By.XPATH, "//*[@class='lyear-radio radio-inline']")[0].click()
        elif bankcard_type == '信用卡':
            driver.find_elements(By.XPATH, "//*[@class='lyear-radio radio-inline']")[1].click()
        driver.find_element(By.XPATH, '//*[@id="cardNum_add_input"]').send_keys(bankcard_num)
        driver.find_element(By.XPATH, '//*[@id="bankCard_save_btn"]').click()
        self.return_bankcard(driver)

    def bankcard_del(self, driver):
        self.switch_bankcard(driver)
        driver.find_elements(By.XPATH, "//*[@class='btn btn-danger delete_btn']")[-1].click()
        time.sleep(1)
        driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(1)
        self.return_bankcard(driver)

    def bankcard_update(self, driver, bankcard_name, bankcard_type, bankcard_num):
        self.switch_bankcard(driver)
        driver.find_elements(By.XPATH, "//*[@class='btn btn-primary update_btn']")[-1].click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="cardBank_update_input"]').clear()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="cardBank_update_input"]').send_keys(bankcard_name)
        if bankcard_type == '借记卡':
            driver.find_elements(By.XPATH, "//*[@class='lyear-radio radio-inline']")[2].click()
        elif bankcard_type == '信用卡':
            driver.find_elements(By.XPATH, "//*[@class='lyear-radio radio-inline']")[3].click()
        driver.find_element(By.XPATH, '//*[@id="cardNum_update_input"]').clear()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="cardNum_update_input"]').send_keys(bankcard_num)
        time.sleep(1)
        driver.find_element(By.XPATH, "//*[@id='bankCard_update_btn']").click()
        self.return_bankcard(driver)

    def bankcard_search(self, driver):
        self.switch_bankcard(driver)
        bankcard_info = driver.find_elements(By.XPATH, "//*[@class='card-header bg-primary']")[-1].text.split('\n')
        bankcard_name = bankcard_info[0].split('(')[0].replace(' ', '')
        bankcard_type = bankcard_info[0].split('(')[1][0:3]
        bankcard_number = driver.find_elements(By.XPATH, "//*[@class='card-body']")[-1].text.split('\n')[0][3:]
        self.return_bankcard(driver)
        return [bankcard_name, bankcard_type, bankcard_number]

    def fund(self, driver, fund_type, pay_password):
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "个人理财").click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, fund_type).click()
        driver.find_element(By.XPATH, "//button[contains(.,\' 投资\')]").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "(//input[@id=\'password\'])[2]").send_keys(pay_password)
        driver.find_element(By.XPATH, "//a[contains(.,\'确定\')]").click()
        time.sleep(1)
        return driver.find_element(By.XPATH, "//*[@class='jconfirm-content']").text

    def loan(self, driver, money, term):
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "金融工具").click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "安全网贷").click()
        driver.find_element(By.ID, "amount").send_keys(money)
        driver.find_element(By.ID, "term").send_keys(term)
        driver.find_element(By.XPATH, "//*[@id=\"submit\"]").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]").click()
        time.sleep(1)

    def loan_count(self, driver):
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "个人中心").click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "我的借贷").click()
        time.sleep(1)
        return int(driver.find_element(By.XPATH, "//button[6]").text[:-1])

    def bankcard_money(self, driver):
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "个人中心").click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "银行卡管理").click()
        time.sleep(1)
        card = driver.find_element(By.XPATH, "//*[@class='card-body']").text.split('\n')
        return float(card[1][5:-1])

    def loan_approve(self, driver):
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "网贷管理").click()
        time.sleep(1)
        driver.find_element(By.LINK_TEXT, "网贷审核").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "//button[contains(.,\' 审核通过\')]").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "//button[contains(.,\'确认\')]").click()
